package com.yami.shop.listener;

import com.yami.shop.bean.event.PaySuccessOrderNftEvent;
import com.yami.shop.bean.model.BusinessNft;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.order.PaySuccessOrderOrder;
import com.yami.shop.service.BusinessNftService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 确认订单信息时的默认操作
 *
 * @author LGH
 */
@Slf4j
@Component("defaultPayOrderNftListener")
@AllArgsConstructor
public class PayOrderNftListener {

    @Autowired
    private BusinessNftService businessNftService;

    /**
     * 购买持仓增加取币记录
     */
    @EventListener(PaySuccessOrderNftEvent.class)
    @Order(PaySuccessOrderOrder.DEFAULT)
    public void defaultSubmitOrderListener(PaySuccessOrderNftEvent event) {
        log.info("*********************************************************购买持仓增加取币记录开始");
        List<com.yami.shop.bean.model.Order> orders = event.getOrders();
        orders.stream().forEach(order -> {
            List<OrderItem> orderItems = order.getOrderItems();
            OrderItem orderItem = orderItems.get(0);
            BusinessNft businessNft = new BusinessNft();
            businessNft.setBusinessType(0);
            businessNft.setCreateTime(new Date());
            businessNft.setOutId(order.getOrderId().toString());
            businessNft.setStatus(0);
            businessNft.setProdId(orderItem.getProdId().toString());
            businessNft.setProdName(orderItem.getProdName());
            businessNft.setNum(orderItem.getProdCount());
            businessNft.setUserId(order.getUserId());
            businessNftService.save(businessNft);
        });
        log.info("*********************************************************购买持仓增加取币记录结束");
    }
}
